﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Utility;
using Data;

namespace Website.UC
{
    public partial class UC_Student : System.Web.UI.UserControl
    {
        Tbl_StudentBus student_bus = new Tbl_StudentBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadData();
            }
        }
        private void loadData()
        {
            GridView1.DataSource = student_bus.getListStudent();
            GridView1.DataBind();
        }
        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            lblId.Text = WebHelper.GetValueGridView(GridView1, 2, e);
            txtName.Text = WebHelper.GetValueGridView(GridView1, 3, e);
            txtPhone.Text = WebHelper.GetValueGridView(GridView1, 5, e);
            txtAddress.Text = WebHelper.GetValueGridView(GridView1, 6, e);
            txtCity.Text = WebHelper.GetValueGridView(GridView1, 7, e);
            txtEmail.Text = WebHelper.GetValueGridView(GridView1, 9, e);
            txtUsername.Text = WebHelper.GetValueGridView(GridView1, 10, e);
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            tbl_student ts = new tbl_student();
            ts.student_id = Int32.Parse(WebHelper.GetDataKey(GridView1, e));
            student_bus.delete(ts);
            loadData();
            clear();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            clear();
        }
        private void clear()
        {
            lblId.Text = "";
            txtName.Text = "";
            txtPhone.Text ="";
            txtAddress.Text = "";
            txtCity.Text = "";
            txtEmail.Text = "";
            txtUsername.Text = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Length == 0)
            {
                WebHelper.ShowMessage("Name not null", Page);
            }
            if (txtPhone.Text.Length == 0)
            {
                WebHelper.ShowMessage("Phone not null", Page);
            }
            if (txtAddress.Text.Length == 0)
            {
                WebHelper.ShowMessage("Address not null", Page);
            }
            if (txtCity.Text.Length == 0)
            {
                WebHelper.ShowMessage("City not null", Page);
            }
            if (txtEmail.Text.Length == 0)
            {
                WebHelper.ShowMessage("Email not null", Page);
            }
            if (txtUsername.Text.Length == 0)
            {
                WebHelper.ShowMessage("Username not null", Page);
            }
            if(!CheckValidate.CheckEmail(txtEmail.Text))
            {
                WebHelper.ShowMessage("Email is not valid", Page);
            }
            else
            {
                tbl_student ts = new tbl_student();
                ts.student_name = txtName.Text;
                ts.birthday = DateTime.Parse(DateSelect1.CalendarDate);
                ts.phone = txtPhone.Text;
                ts.address = txtAddress.Text;
                ts.city = txtCity.Text;
                ts.country = ddlCountry.SelectedValue.ToString();
                ts.username = txtUsername.Text;
                ts.password = "password";
                ts.email = txtEmail.Text;
                if (lblId.Text.Length == 0)
                {
                    student_bus.insertStudent(ts);
                }
                else
                {
                    ts.student_id = Int32.Parse(lblId.Text);
                    student_bus.updateStudent(ts);
                }
              
                loadData();
            }
        }
        }
    
}